.TH std::reference_wrapper::get,std::reference_wrapper::operatorT& 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::reference_wrapper::get,std::reference_wrapper::operatorT& \- std::reference_wrapper::get,std::reference_wrapper::operatorT&

.SH Synopsis
   operator T& () const noexcept; \fB(1)\fP \fI(since C++11)\fP
                                      (constexpr since C++20)
   T& get() const noexcept;       \fB(2)\fP \fI(since C++11)\fP
                                      (constexpr since C++20)

   Returns the stored reference.

.SH Parameters

   \fI(none)\fP

.SH Return value

   The stored reference.

.SH Example


// Run this code

 #include <cassert>
 #include <functional>
 #include <map>
 #include <optional>
 #include <string_view>

 using Map = std::map<std::string_view, int>;
 using Opt = std::optional<std::reference_wrapper<Map::value_type>>;

 Opt find(Map& m, std::string_view s)
 {
     auto it = m.find(s);
     return it == m.end() ? Opt{} : Opt{*it};
 }

 int main()
 {
     Map m{{"A", 1}, {"B", 2}, {"C", 3}};

     if (auto opt = find(m, "C"); opt)
         opt->get().second = 42;
         // std::optional::operator->() returns reference to std::reference_wrapper, then
         // reference_wrapper::get() returns reference to map::value_type, i.e. std::pair

     assert(m["C"] == 42);
 }

.SH See also

   operator() calls the stored function
              \fI(public member function)\fP
